package com.pjhouse.core.common.util;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.log4j.Logger;

import com.pjhouse.core.common.domain.Result;


/**
 * 异常处理辅助类
 */
public class ExceptionUtils {
    private static final Logger LOG = Logger.getLogger(ExceptionUtils.class);

	public static <T> Result<T> handleException(Throwable th) {
        LOG.error("unknown error throw ", th);
        return Result.exceptionInfo(getDetailedMessage(th));
    }
	
	   /**
     * Gets the stack trace of a <code>Throwable</code> in String form.
     * 
     * @param a
     *            Throwable object.
     * @return <code>String</code> Returns the message as String.
     *  
     */
    public static String getDetailedMessage(Throwable a) {
        StringBuffer msg = new StringBuffer();

        msg.append("Message : ");
        msg.append(a.getMessage());
        msg.append("\n");
        msg.append("Exception Stack Trace\n");
        try {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            a.printStackTrace(pw);
            msg.append(sw.toString());
            sw.close();
        } catch (Exception e) {
            msg.append(a.toString());
        }
        String ret = msg.toString();
        msg = null;
        return ret;
    }
}
